home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d16 / winsrc.arc / MATHTOOL.C < prev    next >
C/C++ Source or Header  |  1991-06-16  |  15KB  |  547 lines

  1. /* MathTools.c was written by:
  2.  
  3.       Mark C. Peterson
  4.       The Yankee Programmer
  5.       405-C Queen Street, Suite #181
  6.       Southington, CT 06489
  7.       (203) 276-9721
  8.  
  9.    If you make any changes to this file, please comment out the older
  10.    code with your name, date, and a short description of the change.
  11.  
  12.    Thanks!
  13.  
  14.                                  -Mark
  15.  
  16. */
  17.  
  18. #include <windows.h>
  19. #include "winfract.h"
  20. #include "mathtool.h"
  21. #include <math.h>
  22. #include <stdlib.h>
  23. #include "profile.h"
  24.  
  25. static char MTClassName[] =     "FFWMathTools";
  26. static char MTWindowTitle[] =   "Math Tools";
  27. HWND hFractalWnd, hMathToolsWnd, hCoordBox, hZoomDlg, hZoomBar;
  28. HANDLE hThisInst, hZoomBrush, hZoomPen;
  29. long FAR PASCAL MTWndProc(HWND, WORD, WORD, DWORD);
  30. int MTWindowOpen = 0, CoordBoxOpen = 0, KillCoordBox = 0;
  31. int ZoomBarOpen = 0, KillZoomBar = 0, Zooming = 0, TrackingZoom = 0;
  32. POINT ZoomClick, ZoomCenter;
  33. int ZoomBarMax = 100, ZoomBarMin = -100;
  34. int Sizing = 1, ReSizing = 0;
  35.  
  36. static double Pi =  3.14159265359;
  37. FARPROC DefZoomProc;
  38.  
  39. RECT ZoomRect;
  40.  
  41. WORD CoordFormat = IDM_RECT;
  42. WORD AngleFormat = IDM_DEGREES;
  43.  
  44. extern unsigned xdots, ydots;
  45. extern double xxmin, yymin, xxmax, yymax, delxx, delyy;
  46. extern int ytop, ybottom, xleft, xright;
  47. extern BOOL zoomflag;
  48. extern int time_to_restart, time_to_cycle, time_to_reinit, calc_status;
  49. extern int win_xoffset, win_yoffset;
  50.  
  51. void XorZoomBox(void);
  52.  
  53.  
  54.  
  55.  
  56. /* Global Maintenance */
  57.  
  58. void CheckMathTools(void) {
  59.    if(KillCoordBox) {
  60.       KillCoordBox = 0;
  61.       SaveParamSwitch(CoordBoxStr, FALSE);
  62.       CheckMenuItem(GetMenu(hFractalWnd), IDM_COORD, MF_UNCHECKED);
  63.       DestroyWindow(hCoordBox);
  64.    }
  65.    if(KillZoomBar) {
  66.       XorZoomBox();
  67.       Zooming = 0;
  68.       KillZoomBar = 0;
  69.       SaveParamSwitch(ZoomBoxStr, FALSE);
  70.       CheckMenuItem(GetMenu(hFractalWnd), IDM_ZOOM, MF_UNCHECKED);
  71.       DestroyWindow(hZoomDlg);
  72.    }
  73. }
  74.  
  75. BOOL RegisterMathWindows(HANDLE hInstance) {
  76.     WNDCLASS  wc;
  77.  
  78.     SetToolsPath();
  79.     hThisInst = hInstance;
  80.  
  81.     wc.style = CS_OWNDC;
  82.     wc.lpfnWndProc = MTWndProc;
  83.     wc.cbClsExtra = 0;
  84.     wc.cbWndExtra = 0;
  85.     wc.hInstance = hInstance;
  86.     wc.hIcon = LoadIcon(hInstance, "MathToolIcon");
  87.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  88.     wc.hbrBackground = GetStockObject(BLACK_BRUSH);
  89.     wc.lpszMenuName =  "MathToolsMenu";
  90.     wc.lpszClassName = MTClassName;
  91.  
  92.     return (RegisterClass(&wc));
  93. }
  94.  
  95.  
  96. /* Window Sizing */
  97.  
  98. void SizeWindow(HWND hWnd) {
  99.    if(Sizing) {
  100.       POINT w;
  101.  
  102.       w.x = xdots + (GetSystemMetrics(SM_CXFRAME) * 2);
  103.       w.y = ydots + (GetSystemMetrics(SM_CYFRAME) * 2) +
  104.                      GetSystemMetrics(SM_CYCAPTION) +
  105.                      GetSystemMetrics(SM_CYMENU);
  106.       ReSizing = 1;
  107.       ShowScrollBar(hWnd, SB_BOTH, FALSE);
  108.       SetWindowPos(hWnd, GetNextWindow(hWnd, GW_HWNDPREV), 0, 0,
  109.                    w.x, w.y, SWP_NOMOVE);
  110.       ReSizing = 0;
  111.    }
  112. }
  113.  
  114. void ReSizeWindow(HWND hWnd) {
  115.    if(Sizing) {
  116.       SizeWindow(hWnd);
  117.       ShowWindow(hWnd, SW_SHOWNA);
  118.    }
  119. }
  120.  
  121. void WindowSizing(HWND hWnd) {
  122.    if(Sizing) {
  123.       CheckMenuItem(GetMenu(hWnd), IDM_SIZING, MF_UNCHECKED);
  124.       Sizing = 0;
  125.       ShowScrollBar(hWnd, SB_BOTH, TRUE);
  126.    }
  127.    else {
  128.       CheckMenuItem(GetMenu(hWnd), IDM_SIZING, MF_CHECKED);
  129.       Sizing = 1;
  130.       ReSizeWindow(hWnd);
  131.    }
  132.    ProgStr = Winfract;
  133.    SaveParamSwitch(WindowSizingStr, Sizing);
  134. }
  135.  
  136.  
  137.  
  138. /* Zoom Box */
  139.  
  140. void XorZoomBox(void) {
  141.    HDC hDC;
  142.    HANDLE hOldBrush, hOldPen;
  143.    int OldMode;
  144.  
  145.    if(Zooming) {
  146.       hDC = GetDC(hFractalWnd);
  147.  
  148.       hOldBrush = SelectObject(hDC, hZoomBrush);
  149.       hOldPen = SelectObject(hDC, hZoomPen);
  150.       OldMode = SetROP2(hDC, R2_XORPEN);
  151.  
  152.       Rectangle(hDC, ZoomRect.left, ZoomRect.top, ZoomRect.right,
  153.                 ZoomRect.bottom);
  154.  
  155.       SelectObject(hDC, hOldBrush);
  156.       SelectObject(hDC, hOldPen);
  157.       SetROP2(hDC, OldMode);
  158.  
  159.       ReleaseDC(hFractalWnd, hDC);
  160.    }
  161. }
  162.  
  163. void PositionZoomBar(void) {
  164.    char Temp[15];
  165.  
  166.    SetScrollPos(hZoomBar, SB_CTL, Zooming, TRUE);
  167.    sprintf(Temp, "%d%%", Zooming);
  168.    SetDlgItemText(hZoomDlg, ID_PERCENT, Temp);
  169. }
  170.  
  171. void CalcZoomRect(void) {
  172.    unsigned Midx, Midy;
  173.    double z;
  174.  
  175.    if(Zooming) {
  176.       Midx = xdots >> 1;
  177.       Midy = ydots >> 1;
  178.       z = 1.0 - (fabs((double)Zooming / 100) * .99);
  179.       ZoomRect.left = ZoomCenter.x - (unsigned)(z * Midx) - 1;
  180.       ZoomRect.right = ZoomCenter.x + (unsigned)(z * Midx) + 1;
  181.       ZoomRect.top = ZoomCenter.y - (unsigned)(z * Midy) - 1;
  182.       ZoomRect.bottom = ZoomCenter.y + (unsigned)(z * Midy) + 1;
  183.    }
  184. }
  185.  
  186. void CenterZoom(void) {
  187.    ZoomCenter.x = xdots >> 1;
  188.    ZoomCenter.y = ydots >> 1;
  189. }
  190.  
  191. void StartZoomTracking(DWORD lp) {
  192.    HRGN hRgn;
  193.  
  194.    ZoomClick = MAKEPOINT(lp);
  195.    if(hRgn = CreateRectRgn(ZoomClick.x-1, ZoomClick.y-1, ZoomClick.x+1,
  196.                ZoomClick.y+1)) {
  197.       if(RectInRegion(hRgn, &ZoomRect))
  198.          TrackingZoom = TRUE;
  199.       DeleteObject(hRgn);
  200.    }
  201. }
  202.  
  203. void TrackZoom(DWORD lp) {
  204.    POINT NewPoint, Delta;
  205.  
  206.    XorZoomBox();
  207.  
  208.    NewPoint = MAKEPOINT(lp);
  209.    ZoomCenter.x += NewPoint.x - ZoomClick.x;
  210.    ZoomCenter.y += NewPoint.y - ZoomClick.y;
  211.    ZoomClick = NewPoint;
  212.  
  213.    CalcZoomRect();
  214.    XorZoomBox();
  215. }
  216.  
  217. void EndZoom(DWORD lp) {
  218.    TrackZoom(lp);
  219.    TrackingZoom = FALSE;
  220. }
  221.  
  222. void PaintMathTools(void) {
  223.    XorZoomBox();
  224. }
  225.  
  226. void CancelZoom(void) {
  227.    XorZoomBox();
  228.    CenterZoom();
  229.    TrackingZoom = Zooming = FALSE;
  230.    PositionZoomBar();
  231. }
  232.  
  233. void ExecuteZoom(void) {
  234.    double xd, yd, z;
  235.  
  236.    xd = xxmin + (delxx * ZoomCenter.x);
  237.    yd = yymax - (delyy * ZoomCenter.y);
  238.  
  239.    z = 1.0 - fabs((double)Zooming / 100 * .99);
  240.    if(Zooming > 0)
  241.       z = 1.0 / z;
  242.  
  243.    xxmin = xd - (delxx * z * (xdots / 2));
  244.    xxmax = xd + (delxx * z * (xdots / 2));
  245.    yymin = yd - (delyy * z * (ydots / 2));
  246.    yymax = yd + (delyy * z * (ydots / 2));
  247.  
  248.    time_to_reinit = 1;
  249.    time_to_cycle = 0;
  250.    calc_status = 0;
  251. }
  252.  
  253. BOOL FAR PASCAL ZoomBarProc(HWND hWnd, WORD Message, WORD wp, DWORD dp) {
  254.    switch(Message) {
  255.       case WM_KEYDOWN:
  256.          switch(wp) {
  257.             case VK_RETURN:
  258.                if(TrackingZoom)
  259.                   ExecuteZoom();
  260.                break;
  261.             case VK_ESCAPE:
  262.                CancelZoom();
  263.                break;
  264.             default:
  265.                break;
  266.          }
  267.          break;
  268.    }
  269.    return(CallWindowProc(DefZoomProc, hWnd, Message, wp, dp));
  270. }
  271.  
  272. BOOL FAR PASCAL ZoomBarDlg(HWND hDlg, WORD Message, WORD wp, DWORD dp) {
  273.    FARPROC lpFnct;
  274.    HMENU hDlgMenu;
  275.  
  276.    hDlgMenu = GetMenu(hDlg);
  277.    switch(Message) {
  278.       case WM_INITDIALOG:
  279.          ZoomBarOpen = TRUE;
  280.          ProgStr = Winfract;
  281.          SaveParamSwitch(ZoomBoxStr, TRUE);
  282.          CenterZoom();
  283.          CheckMenuItem(GetMenu(hFractalWnd), IDM_ZOOM, MF_CHECKED);
  284.          PositionWindow(hDlg, ZoomBoxPosStr);
  285.          hZoomDlg = hDlg;
  286.          hZoomBrush = GetStockObject(BLACK_BRUSH);
  287.          hZoomPen = GetStockObject(WHITE_PEN);
  288.          if(!(lpFnct = MakeProcInstance(ZoomBarProc, hThisInst)))
  289.             return(FALSE);
  290.          if(!(hZoomBar = CreateWindow("scrollbar", NULL, WS_CHILD |
  291.                            WS_TABSTOP | SBS_VERT,
  292.                            38, 28, 18, 248, hDlg, NULL, hThisInst, NULL)))
  293.             return(FALSE);
  294.  
  295.          SetScrollRange(hZoomBar, SB_CTL, ZoomBarMin, ZoomBarMax, FALSE);
  296.          SetScrollPos(hZoomBar, SB_CTL, 0, FALSE);
  297.          ShowScrollBar(hZoomBar, SB_CTL, TRUE);
  298.          Zooming = 0;
  299.  
  300.          /* Create a Window Subclass */
  301.          DefZoomProc = (FARPROC)GetWindowLong(hZoomBar, GWL_WNDPROC);
  302.          SetWindowLong(hZoomBar, GWL_WNDPROC, (LONG)lpFnct);
  303.          return(TRUE);
  304.       case WM_MOVE:
  305.          SaveWindowPosition(hDlg, ZoomBoxPosStr);
  306.          break;
  307.       case WM_CLOSE:
  308.          KillZoomBar = 1;
  309.          ProgStr = Winfract;
  310.          break;
  311.       case WM_DESTROY:
  312.          ZoomBarOpen = 0;
  313.          break;
  314.       case WM_VSCROLL:
  315.          if(Zooming)
  316.             XorZoomBox();
  317.          switch(wp) {
  318.             case SB_PAGEDOWN:
  319.                Zooming += 20;
  320.             case SB_LINEDOWN:
  321.                Zooming = min(ZoomBarMax, Zooming + 1);
  322.                break;
  323.             case SB_PAGEUP:
  324.                Zooming -= 20;
  325.             case SB_LINEUP:
  326.                Zooming = max(ZoomBarMin, Zooming - 1);
  327.                break;
  328.             case SB_TOP:
  329.                Zooming = ZoomBarMin;
  330.                break;
  331.             case SB_BOTTOM:
  332.                Zooming = ZoomBarMax;
  333.                break;
  334.             case SB_THUMBPOSITION:
  335.             case SB_THUMBTRACK:
  336.                Zooming = LOWORD(dp);
  337.                break;
  338.          }
  339.          PositionZoomBar();
  340.          CalcZoomRect();
  341.          XorZoomBox();
  342.          break;
  343.       default:
  344.          break;
  345.    }
  346.    return(FALSE);
  347. }
  348.  
  349. void ZoomBar(HWND hWnd) {
  350.    FARPROC lpFnct;
  351.  
  352.    hFractalWnd = hWnd;
  353.    if(ZoomBarOpen)
  354.       KillZoomBar = TRUE;
  355.    else {
  356.       if(lpFnct = MakeProcInstance(ZoomBarDlg, hThisInst)) {
  357.          if(CreateDialog(hThisInst, "ZoomBar", hWnd, lpFnct))
  358.             return;
  359.       }
  360.       MessageBox(hWnd, "Error Opening Zoom Bar",
  361.                  NULL, MB_ICONEXCLAMATION | MB_OK);
  362.    }
  363. }
  364.  
  365.  
  366. /* Coordinate Box */
  367.  
  368. BOOL FAR PASCAL CoordBoxDlg(HWND hDlg, WORD Message, WORD wp, DWORD dp) {
  369.    HMENU hDlgMenu;
  370.  
  371.    hDlgMenu = GetMenu(hDlg);
  372.    switch(Message) {
  373.       case WM_INITDIALOG:
  374.          CoordBoxOpen = 1;
  375.          ProgStr = Winfract;
  376.          SaveParamSwitch(CoordBoxStr, TRUE);
  377.          CheckMenuItem(GetMenu(hFractalWnd), IDM_COORD, MF_CHECKED);
  378.          hCoordBox = hDlg;
  379.          PositionWindow(hDlg, CoordBoxPosStr);
  380.          return(TRUE);
  381.       case WM_MOVE:
  382.          SaveWindowPosition(hDlg, CoordBoxPosStr);
  383.          break;
  384.       case WM_CLOSE:
  385.          KillCoordBox = 1;
  386.          ProgStr = Winfract;
  387.          break;
  388.       case WM_DESTROY:
  389.          CoordBoxOpen = 0;
  390.          break;
  391.       case WM_COMMAND:
  392.          CheckMenuItem(hDlgMenu, AngleFormat, MF_UNCHECKED);
  393.          CheckMenuItem(hDlgMenu, CoordFormat, MF_UNCHECKED);
  394.          switch(wp) {
  395.             case IDM_RADIANS:
  396.             case IDM_GRAD:
  397.             case IDM_DEGREES:
  398.                AngleFormat = wp;
  399.                break;
  400.             case IDM_POLAR:
  401.             case IDM_RECT:
  402.             case IDM_PIXEL:
  403.                CoordFormat = wp;
  404.                break;
  405.          }
  406.          CheckMenuItem(hDlgMenu, AngleFormat, MF_CHECKED);
  407.          CheckMenuItem(hDlgMenu, CoordFormat, MF_CHECKED);
  408.          if(CoordFormat == IDM_POLAR) {
  409.             SetDlgItemText(hDlg, ID_X_NAME, "|z|");
  410.             SetDlgItemText(hDlg, ID_Y_NAME, "\xD8");
  411.             EnableMenuItem(hDlgMenu, IDM_DEGREES, MF_ENABLED);
  412.             EnableMenuItem(hDlgMenu, IDM_RADIANS, MF_ENABLED);
  413.             EnableMenuItem(hDlgMenu, IDM_GRAD, MF_ENABLED);
  414.          }
  415.          else {
  416.             SetDlgItemText(hDlg, ID_X_NAME, "x");
  417.             SetDlgItemText(hDlg, ID_Y_NAME, "y");
  418.             EnableMenuItem(hDlgMenu, IDM_DEGREES, MF_DISABLED | MF_GRAYED);
  419.             EnableMenuItem(hDlgMenu, IDM_RADIANS, MF_DISABLED | MF_GRAYED);
  420.             EnableMenuItem(hDlgMenu, IDM_GRAD, MF_DISABLED | MF_GRAYED);
  421.          }
  422.    }
  423.    return(FALSE);
  424. }
  425.  
  426. void UpdateCoordBox(DWORD dw) {
  427.    unsigned xPixel, yPixel;
  428.    double xd, yd, Angle, Modulus;
  429.    char xStr[40], yStr[40];
  430.  
  431.    xPixel = (unsigned)dw;
  432.    yPixel = (unsigned)(dw >> 16);
  433.    xd = xxmin + (delxx * xPixel);
  434.    yd = yymax - (delyy * yPixel);
  435.    switch(CoordFormat) {
  436.       case IDM_PIXEL:
  437.          sprintf(xStr, "%d", xPixel);
  438.          sprintf(yStr, "%d", yPixel);
  439.          break;
  440.       case IDM_RECT:
  441.          sprintf(xStr, "%+.8g", xd);
  442.          sprintf(yStr, "%+.8g", yd);
  443.          break;
  444.       case IDM_POLAR:
  445.          Modulus = (xd*xd) + (yd*yd);
  446.          if(Modulus > 1E-20) {
  447.             Modulus = sqrt(Modulus);
  448.             Angle = atan2(yd, xd);
  449.             switch(AngleFormat) {
  450.                case IDM_DEGREES:
  451.                   Angle = (Angle / Pi) * 180;
  452.                   break;
  453.                case IDM_GRAD:
  454.                   Angle = (Angle / Pi) * 200;
  455.                case IDM_RADIANS:
  456.                   break;
  457.             }
  458.          }
  459.          else {
  460.             Modulus = 0.0;
  461.             Angle = 0.0;
  462.          }
  463.          sprintf(xStr, "%+.8g", Modulus);
  464.          sprintf(yStr, "%+.8g", Angle);
  465.          break;
  466.    }
  467.    SetDlgItemText(hCoordBox, ID_X_COORD, xStr);
  468.    SetDlgItemText(hCoordBox, ID_Y_COORD, yStr);
  469. }
  470.  
  471. void CoordinateBox(HWND hWnd) {
  472.    FARPROC lpCoordBox;
  473.  
  474.    hFractalWnd = hWnd;
  475.    if(CoordBoxOpen)
  476.       KillCoordBox = TRUE;
  477.    else {
  478.       if(lpCoordBox = MakeProcInstance(CoordBoxDlg, hThisInst)) {
  479.          if(CreateDialog(hThisInst, "CoordBox", hWnd, lpCoordBox))
  480.             return;
  481.       }
  482.       MessageBox(hWnd, "Error Opening Coordinate Box",
  483.                  NULL, MB_ICONEXCLAMATION | MB_OK);
  484.    }
  485.    ProgStr = Winfract;
  486. }
  487.  
  488.  
  489.  
  490. /* Math Tools Window - Not Implemented Yet */
  491.  
  492. BOOL OpenMTWnd(void) {
  493.     hMathToolsWnd = CreateWindow(
  494.         MTClassName,
  495.         MTWindowTitle,
  496.         WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,
  497.         CW_USEDEFAULT, CW_USEDEFAULT,
  498.         xdots, ydots,
  499.         hFractalWnd,
  500.         NULL,
  501.         hThisInst,
  502.         NULL
  503.     );
  504.     if(!hMathToolsWnd)
  505.         return(FALSE);
  506.  
  507.     ShowWindow(hMathToolsWnd, SW_SHOWNORMAL);
  508.     UpdateWindow(hMathToolsWnd);
  509.  
  510.     return(TRUE);
  511. }
  512.  
  513. void MathToolBox(HWND hWnd) {
  514.     hFractalWnd = hWnd;
  515.     if(MTWindowOpen)
  516.        DestroyWindow(hMathToolsWnd);
  517.     else {
  518.       if(!OpenMTWnd())
  519.          MessageBox(hWnd, "Error Opening Math Tools Window", NULL,
  520.                     MB_ICONEXCLAMATION | MB_OK);
  521.     }
  522. }
  523.  
  524.  
  525. long FAR PASCAL MTWndProc(HWND hWnd, unsigned Message, WORD wParam, DWORD lParam) {
  526.    switch (Message) {
  527.       case WM_CREATE:
  528.          CheckMenuItem(GetMenu(hFractalWnd), IDM_MATH_TOOLS, MF_CHECKED);
  529.          MTWindowOpen = 1;
  530.          break;
  531.       case WM_COMMAND:
  532.          switch(wParam) {
  533.             case IDM_EXIT:
  534.                DestroyWindow(hWnd);
  535.                break;
  536.          }
  537.          break;
  538.       case WM_DESTROY:
  539.          CheckMenuItem(GetMenu(hFractalWnd), IDM_MATH_TOOLS, MF_UNCHECKED);
  540.          MTWindowOpen = 0;
  541.          break;
  542.       default:
  543.          return(DefWindowProc(hWnd, Message, wParam, lParam));
  544.     }
  545.     return(0L);
  546. }
  547.